package com.zrrd.gmall.cart.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.zrrd.gmall.cart.service.CartInfoService;
import com.zrrd.gmall.common.result.Result;
import com.zrrd.gmall.common.util.AuthContextHolder;
import com.zrrd.gmall.model.cart.CartInfo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/cart")
public class CartInfoController {
    @Autowired
    private CartInfoService cartInfoService;
    //添加购物车
    @PostMapping("/addToCart/{skuId}/{skuNum}")
    public Result addToCart(@PathVariable("skuId") Long skuId,
                            @PathVariable("skuNum")Integer skuNum,
                            HttpServletRequest request){
        String userId = AuthContextHolder.getUserId(request);
        System.out.println("收到用户id=======>"+userId);
        if (StringUtils.isEmpty(userId)){
            userId = AuthContextHolder.getUserTempId(request);
            System.out.println("收到用户tempId=======>"+userId);
        }
        //执行业务层添加购物车
        cartInfoService.addToCart(skuId,userId,skuNum);
        return Result.ok();
    }
    //查询购物车
    @GetMapping("/cartList")
    public Result cartList(HttpServletRequest request){
        //获取id
        String userId = AuthContextHolder.getUserId(request);
        //获取临时用户ID
        String userTempId = AuthContextHolder.getUserTempId(request);
        //业务层查询
        List<CartInfo> cartList = cartInfoService.getCartList(userId, userTempId);
        //返回结果
        return Result.ok(cartList);
    }
    //更新购物车选中状态
    @GetMapping("/checkCart/{skuId}/{isChecked}")
    public Result checkCart(@PathVariable("skuId")Long skuId,
                            @PathVariable("isChecked")Integer isChecked,
                            HttpServletRequest request){
        System.out.println("收到更新状态请求====》"+skuId);
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isEmpty(userId)){
            userId=AuthContextHolder.getUserTempId(request);
        }
        cartInfoService.checkCart(userId,isChecked,skuId);
        return Result.ok();
    }
    //删除指定cartInfo
    @DeleteMapping("/deleteCart/{skuId}")
    public Result deleteCart(@PathVariable("skuId") Long skuId,HttpServletRequest request){
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isEmpty(userId)){
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartInfoService.deleteCartInfo(userId,skuId);
        return Result.ok();
    }
    @GetMapping("/getCartCheckedList/{userId}")
    public List<CartInfo> getCartCheckedList(@PathVariable("userId") String userId){
        return cartInfoService.getCartCheckedList(userId);
    }
    @GetMapping("/loadCartCache/{userId}")
    public Result loadCartCache(@PathVariable("userId") String userId){
        cartInfoService.loadCartCache(userId);
        return Result.ok();
    }

}
